#include "MainWindow.h"
#include "ui_MainWindow.h"


#include <Defines.h>
#include <CampaignItem.h>
#include <ItemModel.h>
#include <ItemGroup.h>
#include <Item.h>



#include <XmlSerializer.h>
#include <FileHandler.h>


#include <QMessageBox>
#include <QFileDialog>
#include <QTreeView>



namespace
{
QString NEW_CAMPAIGN_NAME = "New Campaign";
}



MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    FileHandler::installSerializer(new XmlSerializer());

    ui->setupUi(this);

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_actionSave_As_triggered()
{
    QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"), QDir::currentPath(), tr("Xml Files (*.xml)"));

    if (fileName.isEmpty()) return;

    int curr = ui->tabWidget->currentIndex();
    QString name = ui->tabWidget->tabText(curr);
    CampaignItem* item = CampaignItem::campaign(name);

    if (!item)
    {
        QMessageBox::information(0, tr("Invalid Campaign Name"), tr("Campaign does not exist: %1").arg(name));
        return;
    }

    if (!FileHandler::save(fileName, item))
    {
        QMessageBox::information(0, tr("Save Failed"), tr("Failed to save campaign!"));
    }

    item->setFilePath(fileName);
    item->setDirty(false);

}

void MainWindow::on_actionOpen_triggered()
{
    QString fileName = QFileDialog::getOpenFileName(this, tr("Save As"), QDir::currentPath(), tr("Xml Files (*.xml)"));

    if (fileName.isEmpty()) return;

    CampaignItem* item = qobject_cast<CampaignItem*>(FileHandler::open(fileName));
    if (!item)
    {
        QMessageBox::information(0, tr("Open Failed"), tr("Failed to open campaign!"));
        return;
    }
    item->setFilePath(fileName);
    item->setDirty(false);

    QTreeView* view = new QTreeView(this);
    ui->tabWidget->addTab(view, NEW_CAMPAIGN_NAME);
    view->setModel(item->model());
    view->expandAll();
}

void MainWindow::on_actionNew_triggered()
{
    CampaignItem* item = CampaignItem::add(NEW_CAMPAIGN_NAME);
    item->setDirty(true);

    QTreeView* view = new QTreeView(this);
    ui->tabWidget->addTab(view, NEW_CAMPAIGN_NAME);
    view->setModel(item->model());
    view->expandAll();
}

void MainWindow::on_actionSave_triggered()
{
    int curr = ui->tabWidget->currentIndex();
    QString name = ui->tabWidget->tabText(curr);
    CampaignItem* item = CampaignItem::campaign(name);

    if (!item)
    {
        QMessageBox::information(0, tr("Invalid Campaign Name"), tr("Campaign does not exist: %1").arg(name));
        return;
    }

    QString fileName = item->filePath();
    if (!QFile::exists(fileName))
    {
        on_actionSave_As_triggered();
        return;
    }

    if (!FileHandler::save(fileName, item))
    {
        QMessageBox::information(0, tr("Save Failed"), tr("Failed to save campaign!"));
    }

    item->setDirty(false);
}



void MainWindow::on_tabWidget_tabCloseRequested(int index)
{
    QString name = ui->tabWidget->tabText(index);
    CampaignItem* item = CampaignItem::take(name);

    if (!item)
    {
        QMessageBox::information(0, tr("Invalid Campaign Name"), tr("Campaign does not exist: %1").arg(name));
        return;
    }

    if (!item->isDirty()) return;

    if (QMessageBox::question(0, tr("Save Changed?"), tr("Campaign has changed. Do you want to save it?"), QMessageBox::Yes | QMessageBox::No) == QMessageBox::No) return;

    on_actionSave_triggered();

}
